package zookeeper;


import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;
import org.junit.Test;


public class ZookeeperWatcher   {

    //注意上面的类名字不能和下面的一样，否则会出错
    ZooKeeper z = null;


    @Before
    public void init() throws Exception{
        //连接zookeeper客户端  格式为地址：端口号，固定2181
        z = new ZooKeeper("192.168.88.200:2181,192.168.88.201:2181,192.168.88.202:2181",2000,null);
    }
        //创建节点
    @Test
    public void create() throws Exception{
        // 参数1：要创建的节点路径  参数2：数据  参数3：访问权限  参数4：节点类型
        String cre = z.create("/qq","qi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(cre);
        z.close();
    }
    //修改更新节点内容
    @Test
    public void testUpdate() throws Exception {
        // 参数1：节点路径   参数2：数据    参数3：所要修改的版本，-1代表任何版本
        z.setData("/a", "nihao".getBytes("UTF-8"), -1);
        z.close();
    }
    //删除节点
    @Test
    public void testRm() throws InterruptedException, KeeperException {
         //参数1：节点路径    参数2：所需要修改的版本，-1代表任何版本
        z.delete("/a", -1);
        z.close();
    }

}



